<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/utilities/fpdf/fpdf.php';
	
	$ciRif = $_POST['ciRif'];
	$clientName = $_POST['clientName'];
	$secondName = $_POST['secondName'];
	$firstLastName = $_POST['firstLastName'];
	$secondLastName = $_POST['secondLastName'];
	$emailAddress = $_POST['emailAddress'];
	$address = $_POST['address'];
	$billDateMembers = explode('-', $_POST['billDate']);
	$billDate = $billDateMembers[2] . '.' . $billDateMembers[1] . '.' . 
		substr($billDateMembers[0], 2);
	$billTax = $_POST['tax'];
	$billNumber = $_POST['number'];
	$billControlNumber = $_POST['controlNumber'];
	$products = $_POST['products'];
	
	function getProductsDetails($products) {
		$productsDetails = explode("\n", $products);
		for ($i = 0; $i < count($productsDetails); $i++) {
			$productDetails = explode(", ", $productsDetails[$i]);
			$billProducts['line'][$i] = $i + 1;
			$billProducts['description'][$i] = $productDetails[1];
			
			$productUnits = explode(' ', $productDetails[2]);
			$billProducts['units'][$i] = substr($productUnits[1], 0, 1);
			$billProducts['quantity'][$i] = $productUnits[0];
			
			$productCost = explode(' ', $productDetails[3]);
			$billProducts['unit_cost'][$i] = $productCost[1];
			
			$totalCost = explode(' ', $productDetails[4]);
			$billProducts['total_cost'][$i] = $totalCost[1];
		}
		
		return $billProducts;
	}
	
	$pdf = new FPDF('P', 'cm', 'Letter');
	$pdf -> AddPage();
	$pdf -> Ln(1.5);
	$pdf -> Cell(15.5);	
	$pdf -> SetFont('Arial', 'B', 9);
	$pdf -> SetTextColor(0, 0, 139);
	$pdf -> Cell(2, 0.4, 'DISTRIBUIDORA', 0, 2, 'L');
	$pdf -> Cell(2, 0.4, 'DUCTOBARRA, S.A.', 0, 2, 'L');
	$pdf -> Cell(2, 0.4, 'RIF.: J-30187702-0', 0, 2, 'L');
	$pdf -> Ln(2);
	$pdf -> Cell(15.5);
	$pdf -> SetFont('Arial', '', 9);
	$pdf -> Cell(2.25, 0.5, 'No CONTROL: ', 0, 0, 'L');
	$pdf -> SetFont('Arial', 'B', 10);
	$pdf -> Cell(0.3, 0.5, '00', 0, 1, 'L');
	$pdf -> Cell(15.75);
	$pdf -> SetFont('Times', 'B', 12);
	$pdf -> SetTextColor(255, 0, 0);
	$pdf -> Cell(0.3, 0.4, 'No   ' . $billControlNumber, 0, 1, 'L');
	$pdf -> Image($_SERVER['DOCUMENT_ROOT'] . '/SAGER/gui/images/logo.png', 2, 1.5);
	$pdf -> Ln(1);
	$pdf -> SetFont('Arial', '', 9);
	$pdf -> SetTextColor(0, 0, 0);
	$pdf -> Cell(2);
	$pdf -> Cell(3, 1, 'FACTURA No. ' . $billNumber, 0, 2, 'L');
	$pdf -> Cell(2, 1, 'FECHA: ' . $billDate, 0, 2, 'L');
	$pdf -> Ln(1);
	$pdf -> Cell(2);
	$pdf -> SetFont('Arial', 'U', 9);
	$pdf -> Cell(1.4, 1, 'NOMBRE', 0, 0, 'L');
	$pdf -> SetFont('Arial', '', 9);
	$pdf -> Cell(4, 1, ': ' . $clientName, 0, 0, 'L');
	$pdf -> Cell(6.5);
	$pdf -> SetFont('Arial', 'U', 9);
	$pdf -> Cell(0.5, 1, 'RIF', 0, 0, 'L');
	$pdf -> SetFont('Arial', '', 9);
	$pdf -> Cell(2, 1, ': ' . $ciRif, 0, 1, 'L');
	$pdf -> Cell(2);
	$pdf -> SetFont('Arial', 'U', 9);
	$pdf -> Cell(3, 1, 'DIRECCION FISCAL', 0, 0, 'L');
	$pdf -> SetFont('Arial', '', 9);
	$pdf -> Cell(4, 1, ': ' . $address, 0, 1, 'L');
	$pdf -> Cell(2);
	$pdf -> SetFont('Arial', 'U', 9);
	$pdf -> Cell(1, 1.5, 'RENG', 0, 0, 'L');
	$pdf -> Cell(7.5, 1.5, 'DESCRIPCION', 0, 0, 'C');
	$pdf -> Cell(1.5, 1.5, 'UND', 0, 0, 'L');
	$pdf -> Cell(1.5, 1.5, 'CANT', 0, 0, 'L');
	$pdf -> Cell(2, 1.5, 'Bs/UND', 0, 0, 'L');
	$pdf -> Cell(2, 1.5, 'TOTAL BS', 0, 1, 'L');
	$pdf -> SetFont('Arial', '', 9);
	
	$billTotal = 0;
	$billProducts = getProductsDetails($products);
	for ($i = 0; $i < count($billProducts['line']); $i++) {
		$pdf -> Cell(2);
		if ($billProducts['line'][$i] < 10)
			$pdf -> Cell(1, 0.5, '0' . $billProducts['line'][$i], 0, 0, 'L');
		else
		$pdf -> Cell(1, 0.5, $billProducts['line'][$i], 0, 0, 'L');
		$pdf -> Cell(7.5, 0.5, $billProducts['description'][$i], 0, 0, 'L');
		$pdf -> Cell(1.5, 0.5, '   ' . $billProducts['units'][$i], 0, 0, 'L');
		$pdf -> Cell(1.5, 0.5, '   ' . $billProducts['quantity'][$i], 0, 0, 'L');
		$pdf -> Cell(2, 0.5, $billProducts['unit_cost'][$i], 0, 0, 'L');
		$pdf -> Cell(2, 0.5, $billProducts['total_cost'][$i], 0, 1, 'L');
		$billTotal += $billProducts['total_cost'][$i];
	}
	
	$pdf -> Cell(14.5);
	$pdf -> Cell(2, 0.5, 'S. TOTAL', 0, 0, 'L');
	if (count(explode('.', $billTotal)) != 2)
		$pdf -> Cell(2, 0.5, $billTotal . '.00', 0, 1, 'L');
	else
		$pdf -> Cell(2, 0.5, $billTotal, 0, 1, 'L');
	$pdf -> Cell(14.5);
	$pdf -> Cell(2, 0.5, $billTax . '% IVA', 0, 0, 'L');
	$pdf -> Cell(2, 0.5, $billTotal * ($billTax / 100), 0, 1, 'L');
	$pdf -> Cell(13.5);
	$pdf -> Cell(2, 0.5, 'TOTAL Bs.', 0, 0, 'L');
	$pdf -> Cell(2, 0.5, $billTotal * (1 + $billTax / 100), 0, 1, 'L');
	
	$pdf -> Ln(4);
	$pdf -> Cell(2.5);
	$pdf -> SetFont('Arial', '', 9);
	$pdf -> Cell(6, 0.5, 'SON: ', 0, 1, 'L');
	$pdf -> Ln(2.5);
	$pdf -> Cell(2.5);
	$pdf -> Cell(7, 0.5, 'POR DISTRIBUIDORA DUCTOBARRA, S.A.', 0, 0, 'L');
	$pdf -> Cell(2.25);
	$pdf -> Cell(2, 0.5, 'RECIBIDO CONFORME', 0, 1, 'L');
	$pdf -> Output();
?> 